<?php

namespace app\index\controller;

use app\BaseController;
use think\facade\Cache;
use think\facade\View;
use think\exception\ValidateException;

use app\middleware\Check;
use app\index\validate\User;


class Index extends BaseController
{
    // 开启批量验证
    protected $batchValidate = true;
    // 控制器中间件
    protected $middleware = [Check::class];
    // protected $middleware = [
    // // 目前没看懂这个里边的参数有什么用
    // 	Check::class . ':index' 	=> ['except' 	=> ['hellos'] ],
    //     'Hello' => ['only' 		=> ['hello'] ],
    // ];

    public function demos (){
        $arr_data = [];
        $arr_data[] = $this->request->param('id');
        $arr_data[] = $this->request->scheme();
        $arr_data[] = $this->request->host();
        $arr_data[] = $this->request->port();
        $arr_data[] = $this->request->remotePort();
        $arr_data[] = $this->request->protocol();
        $arr_data[] = $this->request->contentType();
        $arr_data[] = $this->request->domain();
        $arr_data[] = $this->request->subDomain();
        $arr_data[] = $this->request->panDomain();
        $arr_data[] = $this->request->rootDomain();
        $arr_data[] = $this->request->url();
        $arr_data[] = $this->request->baseUrl();
        $arr_data[] = $this->request->query();
        $arr_data[] = $this->request->baseFile();
        $arr_data[] = $this->request->root();
        $arr_data[] = $this->request->rootUrl();
        $arr_data[] = $this->request->pathinfo();
        $arr_data[] = $this->request->ext();
        $arr_data[] = $this->request->time();
        $arr_data[] = $this->request->type();
        $arr_data[] = $this->request->method();
        $arr_data[] = $this->request->rule();
        $arr_data[] = $this->request->controller(); //controller
        $arr_data[] = $this->request->action(); //action
        $arr_data[] = $this->request->param();
        $arr_data[] = $this->request->middleware();
        
        dump($arr_data);
        // return $this->request->hello; // ThinkPHP   中间件执行的方法
    }

    public function index()
    {
        /*
        $data = [
            'name'  => Null,
            'age'  =>  222,
            'email' => 'thinkphp@qq.com',
        ];

        $validate = new User();
        $result = $validate->scene('edit')->check($data);
        dump($validate);
        if($result != true) {
            return json(['code' => 400, 'msg' => $result]);
        }else {
            return json(['code' => 400, 'msg' => $result]);
        }
        */
        try {
            $this->validate(
                    [
                'name'  => 'thinkphpaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaFFFFFFFFFFFFF',
                'age'  =>  222,
                'email' => 'thinkphp@qq.com',
            ],  'app\index\validate\User');
        } catch (ValidateException $e) {
             // 验证失败 
            dump($e->getError()); // 输出错误信息
            dump($e->getKey()); // 验证错误的字段名
        }
        // return view();
    }

    public function hello($name = 'ThinkPHP8')
    {
        return 'Hello,' . $name . '！This is '. $this->request->action();
    }
    public function hellos (){
        $data = [
            'name'  => Null,
            'age'  =>  222,
            'email' => 'thinkphp@qq.com',
        ];
        return json($data);
    }

    public function jsonss()
    {

        $data = ['aa','afada','afa2fe','真好啊'];
        Cache::set('name','韩王');
        echo Cache::get('name');
    	// 输出JSON
        halt(json_encode($data));
        return json($data);
    }

    public function demo()
    {
        /*
        // 模板变量赋值
        View::assign('name','ThinkPHP');
        View::assign('email','thinkphp@qq.com');
        // 或者批量赋值
        View::assign([
            'name'  => 'ThinkPHP',
            'email' => 'thinkphp@qq.com'
        ]);
        
        // 模板输出
        return View::fetch('demo');
        */
        
        return view('index/demo', [
            'name'  => 'ThinkPHP',
            'email' => '3127176862@qq.com'
        ]);
       
        // return View::fetch('index/demo');
    }

}
